#include<iostream>
#include<cstdio>
using namespace std;
const int N = 5e4 + 10;
int p[N], d[N];
int n, k;
int find(int x)
{
	if (x != p[x]) {
		int root = find(p[x]);
		d[x] += d[p[x]];
		p[x] = root;
	}
	return p[x];
}
int main()
{
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++) {
		p[i] = i;
	}
	int ans = 0;
	for (int i = 0; i < k; i++) {
		int o, a, b;
		scanf("%d%d%d", &o, &a, &b);
		if (o == 2 && a == b || a > n || b > n) {
			ans++;
			continue;
		}
		if (o == 1) {
			int px = find(a);
			int py = find(b);
			if (px == py && (d[a] - d[b]) % 3)ans++;
			else if (px != py) {
				p[px] = py;
				d[px] = d[b] - d[a];
			}
		}
		else {
			int px = find(a);
			int py = find(b);
			if (px == py && (d[a] - d[b] - 1) % 3)ans++;
			else if (px != py) {
				p[px] = py;
				d[px] = d[b] - d[a] + 1;
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}